/**
 * Created by lenovo on 2019/6/11.
 */

/**
 *  日期格式化
 *
 * @param formatStr
 *格式 YYYY/yyyy/YY/yy 表示年份
 *MM/M 月份
 *W/w 星期
 *dd/DD/d/D 日期
 *hh/HH/h/H 时间
 *mm/m 分钟
 *ss/SS/s/S 秒
 * @returns {*}
 * @constructor
 */
Date.prototype.Format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    if (this.getFullYear().toString() == '1970') {
        return '';
    }
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));

    var month = this.getMonth() + 1;
    str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
    str = str.replace(/M/g, this.getMonth());

    str = str.replace(/w|W/g, Week[this.getDay()]);

    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());

    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());

    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());

    return str;
};

String.prototype.UrlHandle = function () {
    var s = this;
    s = s.replace(/(&s=)(\w*)(.)(\w*)|(s=)(\w*)(.)(\w*)/g, '');
    s = s.replace(/[$][{]path[}]/g, '../../');
    if (/[?]/g.test(s)) {
        s += '&s=' + Math.random();
    }
    else {
        s += '?s=' + Math.random();
    }
    return s;
};


var ToolUtil = {};
/**用于用户跳转存储*/
ToolUtil.Map = function () {
    var MapC = function () {
        var data = new Array();
        this.put = function (key, value) {
            data.push(key);
            data[key] = value;
        };
        this.get = function (key) {
            return data[key];
        };
        this.remove = function (key) {
            data[key] = null;
            data.Remove(key);
        };
        this.isEmpty = function () {
            return data.length == 0;
        };
        this.size = function () {
            return data.length;
        };
        this.getdata = function () {
            return data;
        };
        this.clear = function () {
            data = [];
        };
    };
    return new MapC();
};
ToolUtil.FormJSON = function (fromname) {
    var form = document.getElementById(fromname);
    var result = {};
    for (var i = 0, max = form.elements.length; i < max; i++) {
        var e = form.elements[i];
        var name = e.name;
        var isarray = false;
        if (e.nodeName == "SELECT") {
            result[name] = e.value;
        }
        switch (e.type) {
            case 'checkbox':
                if (!result[name]) {
                    result[name] = [];
                }
                if (e.checked) {
                    result[name].push(e.value.replace(/(^\s*)|(\s*$)/g, ''));
                }
                break;
            case 'radio':
                // JSON[name] = null;
                if (e.checked) {
                    result[name] = e.value.replace(/(^\s*)|(\s*$)/g, '');
                }
                break;
            case 'textarea':
            case 'hidden':
            case 'password':
            case 'file':
            case 'text':
                result[name] = e.value.replace(/(^\s*)|(\s*$)/g, '');
                break;
            case 'button':
            case 'image':
            case 'reset':
            case 'submit':
            default:
        }//end switch

    }//end for
    return result;
};

ToolUtil.Http = function (obj) {
    var Http = function (obj) {
        var opts = {
            url: '',
            type: 'POST',
            async: true,
            cache: false,
            data: null,
            contentType: 'application/json;charset=UTF-8',
            dataType: null,
            beforeSend: function (xhr) {
                xhr.setRequestHeader('token', ToolUtil.UserData().token);
            },
            success: function (data) {

            },
            error: function (data) {
                layer.alert(data.msg,{icon:5});
                if (data.code === '98' || data.code === '97') {
                    location.href = global.loginUrl;
                }
            }
        };
        for (var o in obj) {
            opts[o] = obj[o];
        }
        $.ajax(opts);
    };
    return new Http(obj);
};

ToolUtil.POST = function (obj) {
    obj.type = 'POST';
    return ToolUtil.Http(obj)
};
ToolUtil.GET = function (obj) {
    obj.type = 'GET';
    return ToolUtil.Http(obj)
};
ToolUtil.PUT = function (obj) {
    obj.type = 'PUT';
    return ToolUtil.Http(obj)
};
ToolUtil.DELETE = function (obj) {
    obj.type = 'DELETE';
    return ToolUtil.Http(obj)
};
//获取用户数据
ToolUtil.UserData = function () {
    if (store.get('user') == null) {
        location.href = global.loginUrl;
        return
    } else {
        return store.get('user').data;
    }
};
ToolUtil.OpenData=function(){
    var methodConfig=  parent.layer.methodConfig;
    if(methodConfig.data){
        return methodConfig.data;
    }
    return null;
};


